package com.example.schoolhelp.fragment.home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.schoolhelp.R;
import com.example.schoolhelp.view.SlidingTabLayout;

/**
 * Created by Administrator on 2017/6/18.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    View layout;
    Context context;
    ViewPager viewPager;
    MyPagerAdapter pagerAdapter;
    SlidingTabLayout slidingTabLayout;
    String[] typeStr;

    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layout = inflater.inflate(R.layout.fragment_home, null);
        context = getActivity();
        initData();
        initView();
        initViewData();
        initEvent();
        return layout;
    }

    private void initData() {
        typeStr = getResources().getStringArray(R.array.content_type);
    }

    private void initView() {
        viewPager = (ViewPager) layout.findViewById(R.id.home_viewpager);
        slidingTabLayout = (SlidingTabLayout) layout.findViewById(R.id.slideTabLayout);


    }

    private void initViewData() {
        pagerAdapter=new MyPagerAdapter(getFragmentManager(), typeStr);
        viewPager.setAdapter(pagerAdapter);
        slidingTabLayout.setViewPager(viewPager);
        slidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.red_neteasy));
    }

    private void initEvent() {

    }

    /**
     * @param isVisibleToUser fragment可见和不可见时触发
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onClick(View v) {

    }

    class MyPagerAdapter extends FragmentPagerAdapter {
        String[] typeData;

        public MyPagerAdapter(FragmentManager fm, String[] typeData) {
            super(fm);
            this.typeData = typeData;
        }


        @Override
        public int getCount() {
            return typeData.length;
        }

        @Override
        public Fragment getItem(int position) {
            return new PostFragment();
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return typeData[position];
        }
    }
}
